package code.structure.module.scene
{
	import code.main.AppCommonData;
	import code.main.AppCommonView;
	import code.main.AppData;
	import code.main.core.mvc.YController;
	import code.structure.manager.assist.ProjectAssistManager;
	import code.structure.manager.sound.MySoundManager;
	import code.structure.module.configs.ConfigModel;
	import code.structure.module.configs.data.ItemConfigData;
	import code.structure.module.scene.model.SceneModel;
	import code.structure.module.scene.view.Scene;
	import code.structure.module.scene.view.iso.YIsoObject;
	import code.structure.module.user.UserModel;
	import code.tools.sound.src.SoundName;
	import code.untils.CommandNameConst;
	import code.untils.SceneConfig;
	
	import com.patience.mvc.context.Command;
	
	
	
	/**
	 *场景控制器 
	 * @author Yuri
	 * 
	 */
	public class SceneController extends YController
	{
		private var _scene:Scene;
		private var _sceneModel:SceneModel;
		private var _configModel:ConfigModel;
		private var _userModel:UserModel;
		public function SceneController()
		{
			super();
		}
		
		/**
		 *启动 
		 * 
		 */
		override public function start():void{
			_sceneModel = new SceneModel();
			(_context.commonData as AppCommonData).sceneModel = _sceneModel;
			_configModel = (_context.commonData as AppCommonData).configModel;
			_userModel = (_context.commonData as AppCommonData).userModel;
			_scene = new Scene();
			_scene.controller = this;
			
			(_context.commonView as AppCommonView).scene = _scene;
			initCommandHandler();
		}
		
		private function initCommandHandler():void{
			register(CommandNameConst.COMMAND_ANALYSIS_MAP_COMPLETE,onNormalCommandHandler);
			register(CommandNameConst.COMMAND_MARKET_BUY_SEED,onNormalCommandHandler);
			register(CommandNameConst.COMMAND_ENTER_FRIEND_HOME,onNormalCommandHandler);
			register(CommandNameConst.COMMAND_RETURN_MYSELF_HOME,onNormalCommandHandler);
			register(CommandNameConst.COMMAND_RECEIVE_PRIZE,onNormalCommandHandler);
			register(CommandNameConst.COMMAND_CLOSE_GO_TIPS_BY_SCENES,onNormalCommandHandler);
			
			//编辑模式
			register(CommandNameConst.COMMAND_TOGGLE_EDIT_FLOOR_LAYER,onEditCommandHandler);
			register(CommandNameConst.COMMAND_RECOVER_MAP_GOODS,onEditCommandHandler);
			register(CommandNameConst.COMMAND_SELL_MAP_GOODS,onEditCommandHandler);
			register(CommandNameConst.COMMAND_MOVE_MAP_GOODS,onEditCommandHandler);
			register(CommandNameConst.COMMAND_ROTATION_MAP_GOODS,onEditCommandHandler);
			register(CommandNameConst.COMMAND_CONFIRM_EDIT,onEditCommandHandler);
			register(CommandNameConst.COMMAND_MARKET_BUY,onEditCommandHandler);
			register(CommandNameConst.COMMAND_CANCEL_DIY_MOVE,onEditCommandHandler);
			register(CommandNameConst.COMMAND_ENTER_DIY_EDIT,onEditCommandHandler);
			register(CommandNameConst.COMMAND_SHOW_STORAGE,onEditCommandHandler);
			register(CommandNameConst.COMMAND_SHOW_SHOP,onEditCommandHandler);
			
			//镜头设定
			register(CommandNameConst.COMMAND_SCENE_FOCUS_ISOITEM,onCameraCommandHandler);
			register(CommandNameConst.COMMAND_SCENE_FOCUS_AREA,onCameraCommandHandler);
			//场景设置
			register(CommandNameConst.COMMAND_CHANGE_GAME_SCREEN_STATE,onSettingCommandHandler);			
		}
		
		/**
		 *镜头移动 
		 * 
		 */
		private function onCameraCommandHandler(c:Command):void
		{
			if(!_scene || !_scene.isoWorld) return;
			switch(c.cmdName){
				case CommandNameConst.COMMAND_SCENE_FOCUS_ISOITEM:
					var targetIso:YIsoObject = c.target as YIsoObject;
					if(!AppData.isNew)
						_scene.moveWorldCenterBySceneGridXAndGridZ(targetIso.gridX,targetIso.gridZ,true);
					_scene.isoWorld.normalManager.guideIsoObject(targetIso);
					break;
				case CommandNameConst.COMMAND_SCENE_FOCUS_AREA:
					_scene.isoWorld.normalManager.guideArea(c.target as String);
					break;
			}
		}
		
		/**
		 *编辑diy事件Command 
		 * @param c
		 * 
		 */
		private function onEditCommandHandler(c:Command):void
		{
			if(!_scene || !_scene.isoWorld ) return;
			switch(c.cmdName) {
				case CommandNameConst.COMMAND_ENTER_DIY_EDIT:
					_scene.isoWorld.status = SceneConfig.SCENE_DIY;
					_scene.isoWorld.diyManager.diyType = SceneConfig.SCENE_DIY_NORMAL;
					break;
			}
		}
		
		/**
		 *正常情况下 
		 * @param c
		 * 
		 */
		private function onNormalCommandHandler(c:Command):void{
			switch(c.cmdName) {
				case CommandNameConst.COMMAND_ANALYSIS_MAP_COMPLETE://解析地图完成
					_scene.refreshSceneData(sceneModel.dictionary);
					if (_sceneModel.isMyScene)
						MySoundManager.playMuise(MySoundManager.bMuise, SoundName.SELF_HOME_BG);
					else
						MySoundManager.playMuise(MySoundManager.bMuise, SoundName.FRIEND_HOME_BG);
					mapIsoWorldRefreshComplete();
					break;
				case CommandNameConst.COMMAND_MARKET_BUY_SEED: //买种子
					_scene.isoWorld.exitDiy();
					_scene.isoWorld.normalManager.selectedPlantSeed(c.data as ItemConfigData);
					break;
			}
			
		}

		/**
		 *设置 
		 * @param c
		 * 
		 */
		private function onSettingCommandHandler(c:Command):void{
			switch(c.cmdName) {
				case CommandNameConst.COMMAND_CHANGE_GAME_SCREEN_STATE:
					_scene.changeSceneSizeScale(int(c.data));
					break;
			}
		}
		
		/**
		 *地图信息构建刷新成功 
		 * 
		 */
		private function mapIsoWorldRefreshComplete():void{
			var c:Command;
			if(_sceneModel.isMyScene){
				dispatch(new Command(CommandNameConst.COMMAND_MAP_CREATE_COMPLETE));
			}
			
		}
		
		public function get sceneModel():SceneModel
		{
			return _sceneModel;
		}

		public function get configModel():ConfigModel
		{
			return _configModel;
		}

		/**
		 *用户信息数据 
		 */
		public function get userModel():UserModel
		{
			return _userModel;
		}
	}
}